package com.ruoyi.material.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.workorder.domain.WorkOrder;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.material.domain.Materia;
import com.ruoyi.material.service.IMateriaService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 材料Controller
 * 
 * @author renyuxin
 * @date 2024-05-29
 */
@RestController
@RequestMapping("/material/material")
public class MateriaController extends BaseController
{
    @Autowired
    private IMateriaService materiaService;


    /**
     * 查询材料列表
     */
    @PreAuthorize("@ss.hasPermi('material:material:list')")
    @GetMapping("/list")
    public TableDataInfo list(Materia materia)
    {
        startPage();
        LoginUser loginUser = (LoginUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        Long userId = loginUser.getUserId();
//        List<Materia> list = materiaService.selectMateriaList(materia);

        List<WorkOrder> list = materiaService.selectMateriaListForUser(materia, userId);
        return getDataTable(list);
    }
    /**
     * 查询所有材料列表 不分页
     */
    @GetMapping("/listByTypeId/{materiatypeId}")
    public AjaxResult listByTypeId(@PathVariable("materiatypeId") Long materiatypeId)
    {
        return success(materiaService.selectMateriaListByTypeId(materiatypeId));
    }

    /**
     * 导出材料列表
     */
    @PreAuthorize("@ss.hasPermi('material:material:export')")
    @Log(title = "材料", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Materia materia)
    {
        List<Materia> list = materiaService.selectMateriaList(materia);
        ExcelUtil<Materia> util = new ExcelUtil<Materia>(Materia.class);
        util.exportExcel(response, list, "材料数据");
    }

    /**
     * 获取材料详细信息
     */
    @PreAuthorize("@ss.hasPermi('material:material:query')")
    @GetMapping(value = "/{materiaId}")
    public AjaxResult getInfo(@PathVariable("materiaId") Long materiaId)
    {
        return success(materiaService.selectMateriaByMateriaId(materiaId));
    }

    /**
     * 新增材料
     */
    @PreAuthorize("@ss.hasPermi('material:material:add')")
    @Log(title = "材料", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Materia materia)
    {
        if(materiaService.checkNameUnique(materia)){
            return error("材料名称重复!");
        }
        return toAjax(materiaService.insertMateria(materia));
    }

    /**
     * 修改材料
     */
    @PreAuthorize("@ss.hasPermi('material:material:edit')")
    @Log(title = "材料", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Materia materia)
    {
        if(materiaService.checkNameUnique(materia)){
            return error("材料名称重复!");
        }
        return toAjax(materiaService.updateMateria(materia));
    }

    /**
     * 删除材料
     */
    @PreAuthorize("@ss.hasPermi('material:material:remove')")
    @Log(title = "材料", businessType = BusinessType.DELETE)
	@DeleteMapping("/{materiaIds}")
    public AjaxResult remove(@PathVariable Long[] materiaIds)
    {

        if(materiaService.hasData(materiaIds)){
             return error("不允许删除,材料采购记录中有关联数据!");
        }
        return toAjax(materiaService.deleteMateriaByMateriaIds(materiaIds));
    }
}
